package gui;

import java.awt.BorderLayout;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.sql.Connection;
import java.sql.Date;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Vector;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

import model.Meetstation;
import model.Meting;
import model.Model;
import dao.DBManager;
import dao.MeetstationDAO;

public class MeetgegevensView extends JPanel {

	private static final long serialVersionUID = 1L;
	private JPanel jPanel = null;
	private JPanel jPanel1 = null;
	private JScrollPane jScrollPane = null;
	private JTable jTable = null;
	private JLabel jLabel = null;
	private JButton updateButton = null;
	
	private Model hetModel = null;	
	
	private DefaultTableModel tableModel = null;
	private JButton nwRowButton = null;
	private JButton deleteRowButton = null;
	/**
	 * This is the default constructor
	 */
	public MeetgegevensView(Model model) {
		super();
		this.hetModel = model;
		
		
		// Luisteraar voor de wijziging van het station
		hetModel.luisteraarToevoegen("station", new PropertyChangeListener()
		{
			public void propertyChange( PropertyChangeEvent evt ) {
				Meetstation station = (Meetstation)evt.getNewValue();
				loadMetingen( station.getStationID() );
			}
		});
		
		initialize();
	}

	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		this.setSize(442, 309);
		this.setLayout(new BorderLayout());
		this.add(getJPanel(), BorderLayout.NORTH);
		this.add(getJPanel1(), BorderLayout.SOUTH);
		this.add(getJScrollPane(), BorderLayout.CENTER);
		
		tableModel = new DefaultTableModel();
		jTable.setModel(tableModel);
		
		// Meta data van de tabel metingen ophalen
		Connection conn = DBManager.getInstance().getConnection();
		try {
			Statement st = conn.createStatement();
			ResultSet rs = st.executeQuery("SELECT * FROM metingen");
			ResultSetMetaData md = rs.getMetaData();
			int columnCount = md.getColumnCount();
			
			// De kolommen van de tabel aanmaken op basis van de metadata
			for( int i=1; i < columnCount; i++ ) {
				tableModel.addColumn(md.getColumnName(i));
			}
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			DBManager.getInstance().close();
		}
	}

	/**
	 * This method initializes jPanel	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getJPanel() {
		if (jPanel == null) {
			GridBagConstraints gridBagConstraints8 = new GridBagConstraints();
			gridBagConstraints8.anchor = GridBagConstraints.WEST;
			gridBagConstraints8.gridx = 0;
			gridBagConstraints8.gridy = 1;
			gridBagConstraints8.insets = new Insets(0, 5, 0, 0);
			gridBagConstraints8.fill = GridBagConstraints.NONE;
			GridBagConstraints gridBagConstraints5 = new GridBagConstraints();
			gridBagConstraints5.gridx = 0;
			gridBagConstraints5.gridy = 4;
			GridBagConstraints gridBagConstraints4 = new GridBagConstraints();
			gridBagConstraints4.gridx = 0;
			gridBagConstraints4.gridy = 1;
			GridBagConstraints gridBagConstraints3 = new GridBagConstraints();
			gridBagConstraints3.gridx = 0;
			gridBagConstraints3.gridy = 2;
			jLabel = new JLabel();
			jLabel.setText("Meetgegevens");
			jLabel.setFont(new Font("Dialog", Font.BOLD, 18));
			jPanel = new JPanel();
			jPanel.setLayout(new GridBagLayout());
			jPanel.add(jLabel, gridBagConstraints8);
		}
		return jPanel;
	}

	/**
	 * This method initializes jPanel1	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getJPanel1() {
		if (jPanel1 == null) {
			GridBagConstraints gridBagConstraints2 = new GridBagConstraints();
			gridBagConstraints2.gridx = 1;
			gridBagConstraints2.fill = GridBagConstraints.VERTICAL;
			gridBagConstraints2.gridy = 0;
			GridBagConstraints gridBagConstraints1 = new GridBagConstraints();
			gridBagConstraints1.gridx = 2;
			GridBagConstraints gridBagConstraints = new GridBagConstraints();
			gridBagConstraints.gridx = 0;
			gridBagConstraints.fill = GridBagConstraints.VERTICAL;
			gridBagConstraints.gridy = 0;
			jPanel1 = new JPanel();
			jPanel1.setLayout(new GridBagLayout());
			jPanel1.add(getUpdateButton(), gridBagConstraints1);
			jPanel1.add(getNwRowButton(), gridBagConstraints);
			jPanel1.add(getDeleteRowButton(), gridBagConstraints2);
		}
		return jPanel1;
	}

	/**
	 * This method initializes jScrollPane	
	 * 	
	 * @return javax.swing.JScrollPane	
	 */
	private JScrollPane getJScrollPane() {
		if (jScrollPane == null) {
			jScrollPane = new JScrollPane();
			jScrollPane.setViewportView(getJTable());
		}
		return jScrollPane;
	}

	/**
	 * This method initializes jTable	
	 * 	
	 * @return javax.swing.JTable	
	 */
	private JTable getJTable() {
		if (jTable == null) {
			jTable = new JTable();
		}
		return jTable;
	}

	/**
	 * This method initializes updateButton	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getUpdateButton() {
		if (updateButton == null) {
			updateButton = new JButton();
			updateButton.setText("UPDATE");
			updateButton.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					System.out.println("actionPerformed()"); 
					// TODO Auto-generated Event stub actionPerformed()
					
				}
			});
		}
		return updateButton;
	}
	
	public void loadMetingen( int station_id ) {
		ArrayList<Meting> metingen = null;
		
		tableModel.getDataVector().clear();
		
		try 
		{
			metingen = (new MeetstationDAO()).getMetingen(station_id);
		} 
		catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		for(int i=0; i<metingen.size(); i++) {
			Vector row = new Vector();
			row.add(metingen.get(i).getStation_id());
			row.add(metingen.get(i).getDatum());
			row.add(metingen.get(i).getWindrichting());
			row.add(metingen.get(i).getGemWindsnelheid());
			row.add(metingen.get(i).getMaxWindsnelheid());
			row.add(metingen.get(i).getMinWindsnelheid());
			row.add(metingen.get(i).getGemTemperatuur());
			row.add(metingen.get(i).getMaxTemperatuur());
			row.add(metingen.get(i).getMinTemperatuur());
			row.add(metingen.get(i).getMinTemperatuur10());
			row.add(metingen.get(i).getGemLuchtdruk());
			row.add(metingen.get(i).getMinLuchtdruk());
			row.add(metingen.get(i).getMaxLuchtdruk());
			row.add(metingen.get(i).getDuurNeerslag());
			row.add(metingen.get(i).getSomNeerslag());
			tableModel.addRow(row);
		}

	}

	/**
	 * This method initializes nwRowButton	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getNwRowButton() {
		if (nwRowButton == null) {
			nwRowButton = new JButton();
			nwRowButton.setText("INSERT ROW");
			nwRowButton.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					System.out.println("actionPerformed()"); 				
					tableModel.insertRow(0, new Vector());
				}
			});
		}
		return nwRowButton;
	}

	/**
	 * This method initializes deleteRowButton	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getDeleteRowButton() {
		if (deleteRowButton == null) {
			deleteRowButton = new JButton();
			deleteRowButton.setText("DELETE SELECTED ROW(S)");
			deleteRowButton.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					System.out.println("actionPerformed()"); 
					
					if( jTable.getSelectedRow() == -1 ) {
						System.out.println("Er is geen rij geselecteerd");
					}
					else {
						int rijen[] = jTable.getSelectedRows();
						for( int i=0; i<rijen.length; i++) {					
							
							//TODO: Verwijder de rij uit de database
							int id = Integer.parseInt( tableModel.getValueAt(rijen[i], 0).toString().trim() );
							java.sql.Date datum = (Date) tableModel.getValueAt(rijen[i], 1);
							
							MeetstationDAO meetstationDAO = new MeetstationDAO();
							
							try {
								meetstationDAO.deleteMeting(id, datum);
							} catch (SQLException e1) {
								// TODO Auto-generated catch block
								e1.printStackTrace();
							}
							// Verwijder de rij uit de tabel
							tableModel.removeRow(rijen[i]);
						}
					}		
				}
			});
		}
		return deleteRowButton;
	}
		
	
}  //  @jve:decl-index=0:visual-constraint="10,10"
